#pragma once

#include"Common.hpp"
#include"PageCache.h"
#include"CentralCache.h"
#include"ThreadCache.h"

static void* ConcurrentAlloc(size_t bytesize) {

	if (bytesize > MAX_BYTES) {
		// pageCache
		size_t npage = SizeClass::RountUp(bytesize) >> PAGE_SHIFT;
		Span* span=PageCache::GetPageCache()->NewSpan(npage);
		span->_objsize = bytesize;
		return  reinterpret_cast<void*>(span->_pageID << PAGE_SHIFT);
	}
	else {
		if (tls_threadcache == nullptr) {
			tls_threadcache = new ThreadCache;
		}
	}
	return tls_threadcache->Allocate(bytesize);
}

static void  ConcurrentFree(void* ptr) {

	Span* span = PageCache::GetPageCache()->MapObjectToSpan(ptr);

	if (span->_objsize <= MAX_BYTES) {
		assert(tls_threadcache);
		tls_threadcache->Deallocate(ptr, span->_objsize);
	}
	else {
		//pageCache
		pageID id = reinterpret_cast<ADDRES_TYPE>(ptr) >> PAGE_SHIFT;
		Span* span = PageCache::GetPageCache()->MapObjectToSpan(ptr);
		PageCache::GetPageCache()->ReleaseSpanToPageCache(span);
	}

}